//
//  CYYNetWorkManager+Delete.m
//  LetsPartyBmob
//
//  Created by rimi on 15/9/9.
//  Copyright (c) 2015年 柴阳阳. All rights reserved.
//

#import "CYYNetWorkManager+Delete.h"

@implementation CYYNetWorkManager (Delete)
#pragma mark - 删除发起的活动
/*
 1）传入活动的ObjectId
 2）给活动一个partyState为  @“已取消”
 3）置空活动发起者deleteForKey
 */
+ (void)deleteSponsorPartyWithObjectId:(NSString *)partyObjectId
                                sucess:(Sucess)sucess
                                  fail:(Fail)fail{
    //取到改活动
    BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    //为该活动添加关注者属性
    [bmobObject setObject:@"已取消" forKey:@"partyState"];
//    [bmobObject deleteForKey:@"partySponsor"];// 不需要把发起者置空
    
    //新建relation对象
    BmobRelation *relation = [[BmobRelation alloc] init];
    [relation removeObject:[BmobObject objectWithoutDatatWithClassName:@"_User" objectId:[CYYNetWorkManager currentUserGet].objectId]];
    //添加关联关系到partyParticipators列中
    [bmobObject addRelation:relation forKey:@"partyAllPeople"];
    [bmobObject addRelation:relation forKey:@"partyParticipators"];
    [bmobObject addRelation:relation forKey:@"partyObservers"];
    
    [bmobObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            sucess(@"活动取消成功");
        }else{
            fail(error);
        }
    }];
}

#pragma mark - 删除关注的活动
/*relation
 1）传入活动的ObjectId
 2)删除活动关注者通过当前登陆的用户objectID
 */
+ (void)deleteObservePartyWithObjectId:(NSString *)partyObjectId
                                sucess:(Sucess)sucess
                                  fail:(Fail)fail{
    //取到改活动
    BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    //新建relation对象
    BmobRelation *relation = [[BmobRelation alloc] init];
    [relation removeObject:[BmobObject objectWithoutDatatWithClassName:@"_User" objectId:[CYYNetWorkManager currentUserGet].objectId]];
    //添加关联关系到partyObservers列中
    [bmobObject addRelation:relation forKey:@"partyObservers"];
    [bmobObject addRelation:relation forKey:@"partyAllPeople"];
    
    [bmobObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            sucess(@"取消关注成功");
        }else{
            fail(error);
        }
    }];
}

#pragma mark - 删除参加的活动(删除参加+删除关注)
/*
 1）传入活动的ObjectId
 2)删除活动参加者通过当前登陆的用户objectID
 */
+ (void)deleteTarkPartInPartyithObjectId:(NSString *)partyObjectId
                                  sucess:(Sucess)sucess
                                    fail:(Fail)fail{
    [CYYNetWorkManager deleteObservePartyWithObjectId:partyObjectId sucess:^(NSString *sucessString) {
        //取到改活动
        BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
        //新建relation对象
        BmobRelation *relation = [[BmobRelation alloc] init];
        [relation removeObject:[BmobObject objectWithoutDatatWithClassName:@"_User" objectId:[CYYNetWorkManager currentUserGet].objectId]];
        //添加关联关系到partyParticipators列中
        [bmobObject addRelation:relation forKey:@"partyParticipators"];
        [bmobObject addRelation:relation forKey:@"partyAllPeople"];
        
        [bmobObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                sucess(@"退出活动参加成功");
            }else{
                fail(error);
            }
        }];
    } fail:^(NSError *error) {
        fail(error);
    }];
}

#pragma mark - 删除已发起人取消的活动
/**
 *  删除已经被发起人取消的活动
 *
 *  @param partyObjectId 活动objectId
 *  @param sucess        删除成功
 *  @param fail          删除失败
 */
+ (void)deleteAlreadyCanceledPartyWithPartyObjectId:(NSString *)partyObjectId
                                             sucess:(Sucess)sucess
                                               fail:(Fail)fail{
    //取到改活动
    BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"Parties" objectId:partyObjectId];
    //新建relation对象
    BmobRelation *relation = [[BmobRelation alloc] init];
    [relation removeObject:[BmobObject objectWithoutDatatWithClassName:@"_User" objectId:[CYYNetWorkManager currentUserGet].objectId]];
    //添加关联关系到partyParticipators列中
    [bmobObject addRelation:relation forKey:@"partyObservers"];
    [bmobObject addRelation:relation forKey:@"partyParticipators"];
    [bmobObject addRelation:relation forKey:@"partyAllPeople"];
    
    [bmobObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            sucess(@"删除活动成功");
        }else{
            fail(error);
        }
    }];
}

@end
